﻿using System;
using System.Globalization;
using System.Linq;
using AEM.Data;

namespace AEM.Common
{
    public static class ModbusMonitoring
    {
        private static ModbusManager modbus;
        public static void Init()
        {
            modbus = new ModbusManager();
            //modbus.ReceiveDataEvent += new ModbusManager.ReceiveDataHandler(modbus_ReceiveDataEvent);
        }
        /// <summary>
        /// Modbus 数据反馈
        /// </summary>
        static void modbus_ReceiveDataEvent(object sender, EventArgs e)
        {
            try
            {
                int liftnum = (sender as CommunicationBase).LiftNum;

                SerialDataEventArgs args = (from s in Config.lastData
                                            where s.LiftNum == liftnum
                                            select s.Data).FirstOrDefault();

                if (null == args) return;
                byte[] data = new Byte[17];
                Buffer.BlockCopy((sender as CommunicationBase).header, 0, data, 0, 5);
                data[5] = 0x0B;
                data[6] = byte.Parse(liftnum.ToString(), NumberStyles.Integer);
                data[7] = 0x03;
                data[8] = 0x08;
                //int high = liftnum / 10;
                //int low = liftnum % 10;
                data[9] = 0x00;// byte.Parse(high.ToString(), NumberStyles.Integer);
                data[10] = 0x00;// byte.Parse(low.ToString(), NumberStyles.Integer);
                char[] c = args.FloorShow.ToCharArray();
                if (c.Length == 1)
                {
                    data[11] = Convert.ToByte('0');
                    data[12] = Convert.ToByte(c[0]);
                }
                else
                {
                    data[11] = Convert.ToByte(c[0]);
                    data[12] = Convert.ToByte(c[1]);
                }

                string tmp = "00";
                if (args.Data.Dirver) tmp += "1";
                else tmp += "0";
                if (args.Data.StandAlone) tmp += "1";
                else tmp += "0";
                if (args.Data.FaultLamp) tmp += "1";
                else tmp += "0";
                if (args.Data.Locked) tmp += "1";
                else tmp += "0";
                if (args.Data.Fire) tmp += "1";
                else tmp += "0";
                if (args.Data.Overhaul) tmp += "1";
                else tmp += "0";
                data[13] = Convert.ToByte(Convert.ToChar(Convert.ToInt32(tmp, 2)));

                tmp = "0000";
                if (!args.Data.DoorClose && !args.Data.DoorOpen) tmp += "10";
                else
                {
                    if (args.Data.DoorClose) tmp += "1";
                    else tmp += "0";
                    if (args.Data.DoorOpen) tmp += "1";
                    else tmp += "0";
                }
                if (args.Data.Descending) tmp += "1";
                else tmp += "0";
                if (args.Data.Rising) tmp += "1";
                else tmp += "0";
                data[14] = Convert.ToByte(Convert.ToChar(Convert.ToInt32(tmp, 2)));

                modbus.Send(data);
            }
            catch { }
        }
    }
}
